//
//  CoworkerAskVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/23.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "CoworkerAskVC.h"
#import "CoworkerAskCell.h"
#import "WorkmateModel.h"
#import "PublicQuestionViewController.h"
#import "DirectionalQuestionViewController.h"

@interface CoworkerAskVC ()<CoworkerAskCellDelegate>

@property (nonatomic, strong) NSMutableArray *workmateArray;

@end

@implementation CoworkerAskVC {
    NSInteger _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"向同事提问";
    self.tableView.backgroundColor = kStyleColor;
    self.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, 129);
    self.tableView.tableHeaderView = self.tableHeaderView;

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CoworkerAskCell class]) bundle:nil] forCellReuseIdentifier:@"CoworkerAskCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self loadWorkmateData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNo += 1;
        [self loadWorkmateData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (IBAction)freedomBtnClick:(id)sender {
    self.freedomSelectBtn.selected = !self.freedomSelectBtn.selected;
    
    if (self.freedomSelectBtn.selected) {
        for (WorkmateModel *model in self.workmateArray) {
            model.isselect = 0;
        }
        
        [self.tableView reloadData];
    }
}

- (IBAction)okBtnClick:(id)sender {
    NSInteger selectIndex = -1;
    for (NSInteger i = 0; i < self.workmateArray.count; i++) {
        WorkmateModel *model = [self.workmateArray objectAtIndex:i];
        if (model.isselect == 1) {
            selectIndex = i;
            break;
        }
    }
    if (selectIndex == -1 && !self.freedomSelectBtn.selected) {
        [CommonUtils showToast:@"请选择提问方式"];
        return;
    }
    
    if (self.freedomSelectBtn.selected) {
        PublicQuestionViewController *public = [[PublicQuestionViewController alloc] init];
        public.companyId = self.companyId;
        [self.navigationController pushViewController:public animated:YES];
    } else {
        WorkmateModel *selectModel = [self.workmateArray objectAtIndex:selectIndex];
        DirectionalQuestionViewController *direct = [[DirectionalQuestionViewController alloc] init];
        direct.workmateModel = selectModel;
        direct.companyId = self.companyId;
        [self.navigationController pushViewController:direct animated:YES];
    }
}

- (void)loadWorkmateData {
    WS(weakSelf)
    [SendRequest getCompanyIncumbentListByCompanyId:self.companyId andPageNo:[NSString stringWithFormat:@"%zd", _pageNo] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"获取在职同事数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [WorkmateModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            if (_pageNo == 1) {
                [weakSelf.workmateArray removeAllObjects];
                [weakSelf.workmateArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
            } else {
                [weakSelf.workmateArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                if (datas.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.workmateArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 116;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CoworkerAskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoworkerAskCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.workmateArray objectAtIndex:indexPath.row];
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

#pragma mark - CoworkerAskCellDelegate

- (void)selectBtnClickWithModel:(WorkmateModel *)model andIndex:(NSInteger)index andIsSelect:(NSInteger)isSelect {
    if (isSelect == 1) {
        self.freedomSelectBtn.selected = NO;
        
        for (WorkmateModel *modelCon in self.workmateArray) {
            if (![modelCon.userId isEqualToString:model.userId]) {
                modelCon.isselect = 0;
            }
        }
        [self.tableView reloadData];
    }
}

- (NSMutableArray *)workmateArray {
    if (!_workmateArray) {
        _workmateArray = [[NSMutableArray alloc] init];
    }
    return _workmateArray;
}

@end
